/**
 * Skills System Tests
 * 
 * Tests for the skills management system
 */

import { describe, test, expect, beforeEach } from 'bun:test';
import { SkillsManager } from '@/skills/skill-manager';
import { builtinSkills } from '@/skills/builtin';
import { dataAnalystSkill } from '@/skills/builtin/data-analyst';

describe('Skills Manager', () => {
  let manager: SkillsManager;

  beforeEach(() => {
    manager = new SkillsManager();
  });

  test('should register a skill', () => {
    manager.registerSkill(dataAnalystSkill);
    
    const skill = manager.getSkill('data-analyst');
    expect(skill).toBeDefined();
    expect(skill?.name).toBe('Data Analyst');
  });

  test('should register multiple skills', () => {
    manager.registerSkills(builtinSkills);
    
    const allSkills = manager.getAllSkills();
    expect(allSkills.length).toBe(builtinSkills.length);
  });

  test('should activate a skill', () => {
    manager.registerSkill(dataAnalystSkill);
    
    const success = manager.activateSkill('data-analyst');
    expect(success).toBe(true);
    
    const activeSkills = manager.getActiveSkills();
    expect(activeSkills.length).toBe(1);
    expect(activeSkills[0].id).toBe('data-analyst');
  });

  test('should deactivate a skill', () => {
    manager.registerSkill(dataAnalystSkill);
    manager.activateSkill('data-analyst');
    
    const success = manager.deactivateSkill('data-analyst');
    expect(success).toBe(true);
    
    const activeSkills = manager.getActiveSkills();
    expect(activeSkills.length).toBe(0);
  });

  test('should get skills by category', () => {
    manager.registerSkills(builtinSkills);
    
    const dataSkills = manager.getSkillsByCategory('data_analysis');
    expect(dataSkills.length).toBeGreaterThan(0);
    expect(dataSkills[0].category).toBe('data_analysis');
  });

  test('should search skills', () => {
    manager.registerSkills(builtinSkills);
    
    const results = manager.searchSkills('data');
    expect(results.length).toBeGreaterThan(0);
  });

  test('should get active tools', () => {
    manager.registerSkill(dataAnalystSkill);
    manager.activateSkill('data-analyst');
    
    const tools = manager.getActiveTools();
    expect(tools.length).toBeGreaterThan(0);
  });

  test('should get active system prompt', () => {
    manager.registerSkill(dataAnalystSkill);
    manager.activateSkill('data-analyst');
    
    const prompt = manager.getActiveSystemPrompt();
    expect(prompt).toBeDefined();
    expect(prompt.length).toBeGreaterThan(0);
  });

  test('should export and import skill', () => {
    manager.registerSkill(dataAnalystSkill);
    
    const exported = manager.exportSkill('data-analyst');
    expect(exported).toBeDefined();
    
    const newManager = new SkillsManager();
    const success = newManager.importSkill(exported!);
    expect(success).toBe(true);
    
    const imported = newManager.getSkill('data-analyst');
    expect(imported).toBeDefined();
    expect(imported?.name).toBe('Data Analyst');
  });

  test('should get statistics', () => {
    manager.registerSkills(builtinSkills);
    manager.activateSkill('data-analyst');
    
    const stats = manager.getStatistics();
    expect(stats.total).toBe(builtinSkills.length);
    expect(stats.active).toBe(1);
    expect(stats.byCategory).toBeDefined();
  });
});

